package common.BaseClasses;

import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;

import common.MyUtils;
import common.PageBean;

/**
 * ck: bundle by
 * 
 * @author CkWorkspace
 * 
 */
public class CommonAction extends ActionSupport implements ServletRequestAware,
		ServletResponseAware {
	public HttpServletRequest request;
	public HttpServletResponse response;
	public PageBean pageBean = new PageBean();// 分页参数Bean
	public Map<String, Object> paramMap = new HashMap<String, Object>();

	public CommonAction() {

	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public Map<String, Object> getParamMap() {
		return paramMap;
	}

	public void setParamMap(Map<String, Object> paramMap) {
		this.paramMap = paramMap;
	}

	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		// TODO Auto-generated method stub
		this.response = arg0;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		this.request = arg0;
	}

	// 重写getParameter方法
	public String getParameter(String arg, String defaultValue) {
		String temp = request.getParameter(arg);
		if (MyUtils.isEmptyStr(temp))
			return defaultValue;
		else
			temp = request.getParameter(arg).trim();
		return temp;
	}

	public String getParameter(String arg) {
		return getParameter(arg, "");
	}

	public int getParameter(String arg, int defaultValue) {
		String temp = request.getParameter(arg);
		if (temp == null)
			return defaultValue;
		else {
			temp = request.getParameter(arg).trim();
			try {
				return Integer.parseInt(temp);
			} catch (Exception e) {
				return defaultValue;
			}
		}
	}

	// 将url中的参数强制装换为integer对象
	public int getIntParameter(String str) {
		try {
			String strParam = request.getParameter(str);
			if (strParam != null && strParam.trim() != "") {
				return Integer.parseInt(strParam);
			} else {
				return 0;
			}
		} catch (Exception e) {
			return 0;
		}

	}

	/**
	 * 将查询得到的分页数据的有关参数值赋给分页参数Bean
	 * 
	 * @param resultMap2
	 */
	public void setPageParam(Map<Object, Object> resultMap) {
		pageBean.setCurrentPage(Integer.parseInt(resultMap.get("currentPage")
				+ ""));
		pageBean.setPageCount(Integer.parseInt(resultMap.get("pageCount") + ""));
		pageBean.setPageSize(Integer.parseInt(resultMap.get("pageSize") + ""));
		pageBean.setRowsCount(Integer.parseInt(resultMap.get("rowsCount") + ""));
	}

	public void writeStringToResponse(Object content) {
		try {
			// 设置字符集
			response.setCharacterEncoding("utf-8");
			// 获取向前台打印的方法
			PrintWriter pw = response.getWriter();
			pw.print(content);
			pw.flush();
			pw.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	/**
	 * ck:将request参数装载为map
	 * 
	 * @param request
	 * @return
	 */
	public Map<String, Object> modelDriverMapInd(HttpServletRequest request) {
		Enumeration<String> ee = request.getParameterNames();
		Map<String, Object> modelMap = new HashMap<String, Object>();
		while (ee.hasMoreElements()) {
			Object object = (Object) ee.nextElement();
			modelMap.put(object + "", request.getParameter(object + ""));
		}
		return modelMap;
	}

}
